/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;
import domain.Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
//import mapping.PlayerMap;
import network.Network;

/**
 *
 * @author Jasper
 */
public class LoginController extends Thread{
    private String error;

    public LoginController(){ 
        
    }
    
    //methode die player aanmaakt --> server (die login aanmaakt en bijhoud) (TCP --> moet zeker toekomen)
    public void login(String naam){
        while(true){
            Socket link = null;
        try{
            //TODO --> hier zit nog een bug
            link = new Socket(InetAddress.getLocalHost(), Network.PORT);
            PrintWriter output = new PrintWriter((link.getOutputStream()),true);
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));

            output.println("login");
            output.flush();
            
            //loginnaam --> server sturen
            String errorMessage;
            output.println(naam);
            output.flush();
            
            //server --> error 
            errorMessage = input.readLine();
            
            //errorMessage niet leeg? --> exception!
            if(!errorMessage.equals("")){
                setError(errorMessage);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }  
        }
    }
    public String getError(){
        return error;
    }
    
    public void setError(String error){
        this.error = error;
    }
    
    public Client getClient(String naam){
        Socket link = null;
        try{
            //TODO --> hier zit nog een bug
            link = new Socket(InetAddress.getLocalHost(), Network.PORT);
            PrintWriter output = new PrintWriter((link.getOutputStream()));
            InputStream is = link.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            
            output.println("getClient");
            output.flush();
            
            //loginnaam --> server sturen
            output.println(naam);
            output.flush();
            
            return (Client)ois.readObject();
        }catch(IOException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }  
        return null;
    }
}
